
import 'dart:math';

import 'package:flutter/material.dart';

void main() =>runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context){
    return MaterialApp(
        home:sliverDemo01()
    );
  }
}

class sliverDemo01 extends StatelessWidget {
  const sliverDemo01({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return HYHomePage();
  }
}

class HYHomePage extends StatefulWidget {
  @override
  State<HYHomePage> createState() => _HYHomePageState();
}

class _HYHomePageState extends State<HYHomePage> {
  ScrollController controller = ScrollController(initialScrollOffset: 100);
  bool isShowFloatingBtn = false;
  @override
  void initState(){
    super.initState();
    controller.addListener(() {
      //print('监听到了滚动》》》》》');
      setState(() {
        isShowFloatingBtn = controller.offset >= 1000;
      });
    });
  }
  Widget build(BuildContext context){
    /*
    * 两种方式可以监听：
    * controller:
    *   1、可以设置某认offset
    *   2、监听滚动，也可以监听滚动的位置
    * NotificationListener
    *   1、开始滚动和结束滚动
    * */
    return Scaffold(
      appBar: AppBar(
        title: Text("列表测试"),
      ),
      body: NotificationListener(
        onNotification: (ScrollNotification notification){
          if(notification is ScrollStartNotification) {
            print("开始滚动");
          } else if(notification is ScrollUpdateNotification) {
            print('正在滚动');
          } else if(notification is ScrollEndNotification) {
            print('结束滚动');
          }
          return true;
        },
        child: ListView.builder(
            controller:controller,
            itemCount: 1000,
            itemBuilder: (BuildContext ctx,int index){

            return ListTile(
              leading: Icon(Icons.people),
              title: Text('联系人$index'),
            );
        }),
      ),
      floatingActionButton: isShowFloatingBtn ? FloatingActionButton(
        child: Icon(Icons.arrow_upward),
        onPressed: (){
          controller.animateTo(0, duration: Duration(seconds: 1), curve: Curves.easeIn);
        },
      ):null,
    );
  }
}








